﻿// <copyright file="TemplateInstaller.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2009-12-26</date>
// <summary>TemplateInstaller code behind</summary>

namespace InstallTemplates
{
    using System.Collections;
    using System.ComponentModel;
    using System.Configuration.Install;
    using System.Diagnostics.CodeAnalysis;
    using System.Globalization;
    using System.Security.Permissions;

    /// <summary>
    /// Visual Studio 2008 Template Installer
    /// </summary>
    /// <remarks>this will call devenv.exe /installvstemplates for Visual 
    /// Studio 2008 and Visual Studio 2010 to update the template cache.  
    /// It should get called at Commit and Uninstall.
    /// These are the properties in the Custom action:
    /// <list type="table">
    /// <listheader>
    /// <term>Property</term>
    /// <description>Value</description>
    /// </listheader>
    /// <item>
    /// <term>Condition</term>
    /// <description>VS2010INSTALLDIR</description>
    /// </item>
    /// <item>
    /// <term>CustomActionData</term>
    /// <description>/InstallDir="[VS2010INSTALLDIR]\"</description>
    /// </item>
    /// <item>
    /// <term>InstallerClass</term>
    /// <description>True</description>
    /// </item>
    /// </list>
    /// </remarks>
    [RunInstaller(true)]
    public partial class TemplateInstaller : Installer
    {
        #region Constructors
        /// <summary>
        /// Initializes a new instance of the TemplateInstaller class
        /// </summary>
        [System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1805:DoNotInitializeUnnecessarily", Justification = "Generated code.")]
        public TemplateInstaller()
        {
            this.InitializeComponent();
        }
        #endregion

        #region Methods
        /// <summary>
        /// Install the templates in Visual Studio
        /// </summary>
        /// <param name="savedState">the saved state</param>
        ////[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "Only called from installer.")]
        [SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        ////[PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        public override void Commit(IDictionary savedState)
        {
            base.Commit(savedState);

            this.InstallVSTemplates();
        }

        /// <summary>
        /// Uninstall the templates in Visual Studio 2008
        /// </summary>
        /// <param name="savedState">the installer's saved state</param>
        [SecurityPermission(System.Security.Permissions.SecurityAction.Demand)]
        ////[PermissionSet(SecurityAction.LinkDemand, Name = "FullTrust")]
        [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes", Justification = "We want to catch everything or else this cannot uninstall.")]
        public override void Uninstall(IDictionary savedState)
        {
            base.Uninstall(savedState);

            try
            {
                this.InstallVSTemplates();
            }
            catch (System.Exception se)
            {
                System.Diagnostics.Debug.WriteLine(se.Message);
            }
        }

        /// <summary>
        /// Update the Visual Studio 2008 Item and Project template cache
        /// </summary>
        //// [SuppressMessage("Microsoft.Security", "CA2122:DoNotIndirectlyExposeMethodsWithLinkDemands", Justification = "This is called from an installer.")]
        private void InstallVSTemplates()
        {
            var installDir = this.Context.Parameters["InstallDir"];

            if (string.IsNullOrEmpty(installDir))
            {
                return;
            }

            var message = string.Format(
                CultureInfo.CurrentCulture,
                "Installing Visual Studio Templates at {0}",
                installDir);

            this.Context.LogMessage(message);

            var devenv = string.Format(
                CultureInfo.InvariantCulture,
                "{0}devenv.exe",
                installDir);

            if (!System.IO.File.Exists(devenv))
            {
                return;
            }

            using (var process = System.Diagnostics.Process.Start(devenv, @"/installvstemplates"))
            {
                process.WaitForExit();

                process.Close();
            }
        }
        #endregion
    }
}
